package com.jxd.ordering.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxd.ordering.dao.EmpMapper;
import com.jxd.ordering.dao.UserMapper;
import com.jxd.ordering.model.Emp;
import com.jxd.ordering.service.IDictService;
import com.jxd.ordering.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName EmpServiceImpl
 * @Description TODO
 * @Author Ankailong
 * @Date 2022/9/25
 * @Version 1.0
 */
@Service
public class EmpServiceImpl implements IEmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IDictService dictService;

    /**
     * 新增员工信息
     * @param emp 员工对象
     * @return 是否新增成功
     */
    @Override
    public boolean addEmp(Emp emp) {
        return empMapper.insertEmp(emp);
    }

    /**
     * 修改员工信息
     *
     * @param emp 员工对象
     * @return 是否修改成功
     */
    @Override
    @Transactional
    public boolean editEmp(Emp emp) {
        //开启事务
        empMapper.updateEmp(emp);
        userMapper.updateUname(emp.getUserid(), emp.getEname());
        return true;
    }

    /**
     * 批量删除员工信息
     * @param empnos 员工编号列表
     * @return 是否删除成功
     */
    @Override
    @Transactional
    public boolean delEmp(int[] empnos) {
        //开启事务
        int[] userids = new int[empnos.length];
        for (int i = 0; i < empnos.length; i++) {
            userids[i] = empMapper.selectEmpByEmpno(empnos[i]).getUserid();
        }

        empMapper.deleteEmp(empnos);
        userMapper.deleteUser(userids);
        return true;
    }

    /**
     * 分页查询员工信息
     * @param queryMap 包含page(页码), limit(每页显示条数)
     * @return 分页查询结果
     */
    @Override
    public Map<String, Object> getAllEmpList(Map<String, String> queryMap) {
        String page = queryMap.get("page");
        String limit = queryMap.get("limit");

        Map<String, Object> map = new HashMap(4);
        if(page != null && limit != null){
            IPage<Emp> pages = new Page(Integer.parseInt(page),Integer.parseInt(limit));
            empMapper.selectEmpByPage(pages, queryMap);

            map.put("count",pages.getTotal());
            map.put("data",pages.getRecords());

            ((List<Map<String, Object>>) (map.get("data"))).stream().map(item -> {
                if (item.get("sex") != null) {
                    item.put("sex", dictService.getDict("sex", item.get("sex").toString()));
                }
                if (item.get("job") != null) {
                    item.put("job", dictService.getDict("job", item.get("job").toString()));
                }
                return item;
            }).collect(Collectors.toList());

            map.put("code",0);
            return map;
        }else{
            map.put("data", (Object)null);
            map.put("code", "500");
            map.put("msg", "参数不符");
            map.put("count", (Object)null);
            return map;
        }
    }

    /**
     * 按员工编号查询员工信息
     * @param empno 员工编号
     * @return 员工对象
     */
    @Override
    public Emp getEmpByEmpno(int empno) {
        return empMapper.selectEmpByEmpno(empno);
    }
}
